package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.model.Notice;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.INoticeService;
import com.kust.smarthome.web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/notice")
@Api(value = "警告相关", tags = {"user-tag"})
public class NoticeController {

    @Resource
    private INoticeService noticeService;

    @Resource
    private IUserService userService;

    @GetMapping("/queryMessage")
    @ApiOperation("获取指定设备信息")//Principal principal
    public List queryMessage(Principal principal){
        User user = userService.getByUsername(principal.getName());
        Integer userId=user.getId();

        List<Notice> l1=noticeService.queryMessage1(userId);
        List<Notice> l2=null;
        for(int i=0;i<l1.size();i++){
            l2=noticeService.queryMessage2(l1.get(i).getTypeId(),l1.get(i).getDeviceId());//获取警告表的content,created
            l1.get(i).setList(l2);
        }
        return l1;
    }

    /*
    *
    * */
    @GetMapping("/updateState")
    @ApiOperation("改变警告状态为1")//Principal principal
    public boolean updateState1ById(Principal principal,Integer typeId,Integer deviceId) {
        User user = userService.getByUsername(principal.getName());
        Integer userId=user.getId();
        try{
            noticeService.updateState(userId,typeId,deviceId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @GetMapping("/getById")
    public Notice getNoticeById(Integer id) {
        return noticeService.getById(id);
    }



   /* @GetMapping("/getNotice")
    @ApiOperation("获取对应用户的警告信息")
    public List<Notice> getNotice(Principal principal) {
        User user = userService.getByUsername(principal.getName());
        return noticeService.getState0ByUser(user.getId());
    }*/

    /*@GetMapping("/updateState1ById")
    @ApiOperation("改变警告状态")
    public boolean updateState1ById(Principal principal,Integer id) {

        User user = userService.getByUsername(principal.getName());
        Notice notice = noticeService.getById(id);
        if (user.getId() == notice.getUserId()) {
            notice.setState(1);
            return noticeService.updateById(notice);
        }

        return false;
    }*/

}
